(function ($, ka) {

	$.extend(ka.view, {
		$gradeObject: null,
		$designProjectTypes: null,
		$square: null,
		$repairEstimate: null,
		$calculateBtn: null,
		$totalLabel: null
	});

	$.extend(ka, {

		showCalcPopup: function () {
			ka.view.$calcPopupContainer
				.show()
				.find('.iconClose')
				.off('click')
				.on('click', function () { ka.view.$calcPopupContainer.hide(); });
			this.setCalcDomOptions();
			this.bindCalculateEvents();
		},

		calculate: function () {
			var view = ka.view,
				square = parseInt(view.$square.val(), 10) || 0,
				price = view.$designProjectTypes.find('option:selected').data('price'),
				gradePrice = view.$gradeObject.is(':checked') ? view.$gradeObject.data('price') : 0,
				estimatePrice = view.$repairEstimate.is(':checked') ? view.$repairEstimate.data('price') : 0,
				total = square * (price + gradePrice + estimatePrice);
			view.$totalLabel.html(total);
		},

		bindCalculateEvents: function() {
			var context = this,
			view = ka.view;
			view.$calculateBtn.off('click').on('click', function() {
				if (context.isNumeric(view.$square.val())) {
					context.calculate();
				}
			});
		},

		setCalcDomOptions: function() {
			var view = ka.view;
			view.$gradeObject = $('#gradeObject');
			view.$designProjectTypes = $('#designProjectTypes');
			view.$square = $('#square');
			view.$repairEstimate = $('#repairEstimate');
			view.$calculateBtn = $('#calculateBtn');
			view.$totalLabel = $('#totalLabel');
		},

		isNumeric: function (value) {
			return !isNaN(parseFloat(value)) && isFinite(value);
		}

	});

})(jQuery, konsolAntresol);